<template>
  <el-dialog
    title="新增员工"
    width="600px"
    :visible="value"
    @close="closeEvent"
  >
    <!-- close:关闭时会执行，在点击X时也会执行 -->
    <!-- .sync它是值的双向绑定   value与el-dialog内的值双向绑定了，el-dialog内是可以修改value值-->
    <el-form label-width="120px" :model="form" :rules="rules" ref="form">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="1-30个字符"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- focus :获取焦点时调用   readonly：原生的只读属性 -->
        <div v-clickOutside="outsideFn">
          <el-input
            readonly
            @focus="focusEvent"
            v-model="form.departmentName"
            placeholder="请输入"
          ></el-input>
          <el-tree
            v-if="showTree"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          ></el-tree>
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <template #footer>
      <div>
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import employeesData from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserPost } from '@/api/employees'
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  model: {
    prop: 'value',
    event: 'input123'
  },
  data () {
    return {
      showTree: false,
      treeData: [], // 组织架构数据
      isShow: false,
      employeesData,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //e	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 30,
            message: '请输入1-30个字符'
          }
        ], //	string	非必须		姓名
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请正确输入手机号',
            trigger: 'change'
          }
        ], //	string	非必须		手机号
        timeOfEntry: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //	string	非必须		入职时间
        formOfEmployment: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //	number	非必须		聘用形式
        workNumber: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 20,
            message: '请输入1-20个字符'
          }
        ], //	string	非必须		工号
        departmentName: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //e	string	非必须		组织名称
        correctionTime: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ] //	string	非必须		转正时间
      }
    }
  },
  methods: {
    closeEvent () {
      this.$emit('input123', false)
      // 清空表单数据移除表单验证
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //e	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      }
    },
    // 确定点击
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await sysUserPost(this.form)
          // 提示
          this.$message.success('新增员工成功')
          // 关闭弹框
          this.closeEvent()
          // 刷新父级列表数据
          this.$emit('getData')
        }
        console.log(result)
      })
    },
    // 调用接口获取组织架构数据
    async focusEvent () {
      this.showTree = true
      // 没有数据时才调用接口
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts)
      }
      // console.log('组织架构数据', )
    },
    // 树形数据转换
    changeData (arr, str = '') {
      // 1:找出最外层的组织
      return arr.filter(item => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 树形控件点击
    nodeClick (data) {
      console.log('data', data)
      this.form.departmentName = data.name
      this.showTree = false
    },
    // 自定义指令处理
    outsideFn () {
      // 关闭el-tree
      if (this.showTree === false) return
      console.log('outsideFn')
      this.showTree = false
    }
  }
}
</script>
<style></style>
